/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.util;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.io.File;

/**
 * 一个轻量级存储工具
 *
 * @since 2021-03-24
 */
public class Shareprefance {
    Context context;
    Preferences preferences = null;
    public static final String loginFlag = "LOGIN_FLAG";//是否登录过
    public static final String startFlag = "START_FLAG";//是否开启过APP
    private static volatile Shareprefance sInstance = null;

    public Shareprefance(Context context) {
        this.context = context;
    }

    public void initDB() {
        DatabaseHelper databaseHelper = new DatabaseHelper(context.getApplicationContext()); // context入参类型为ohos.app.Context。
        String fileName = "FIRST_FLAG"; // fileName表示文件名，其取值不能为空，也不能包含路径，默认存储目录可以通过context.getPreferencesDir()获取。
        preferences = databaseHelper.getPreferences(fileName);
    }

    //增加
    public void insert(String key, Boolean bool) {
//        DatabaseHelper databaseHelper = new DatabaseHelper(context);
//        Preferences preferences = databaseHelper.getPreferences("FIRST_FLAG");
        System.out.println("zhangliang==>" + key + "insert" + bool);
        preferences.putBoolean(key, bool);
        preferences.flush();
    }

    //查询
    public boolean select(String key) {
        boolean string2 = preferences.getBoolean(key, false);
        System.out.println("zhangliang==>" + key + "select" + string2);
        return string2;
    }

    //删除文件后依然能新增和查看
    public void delete() {
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        String fileName = "FIRST_FLAG"; // fileName表示文件名，其取值不能为空，也不能包含路径。
        databaseHelper.deletePreferences(fileName);
    }

}
